/**
 * 付款界面
 * author:zhangjlt
 * date:2017年3月16日13:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var address = {
		provinceList: {},
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			//返回
			$("#return_address").unbind().on("click", function() {
				window.location.href = "#/index/myProfile";
			});
			$(document).off("click", '#detail_address').on("click", '#detail_address', function() {
				if(address.provinceList && address.provinceList.length > 0) {
					address.initPicker();
				}
			});
			$(document).off("click", '#save_address').on("click", '#save_address', function() {
				var addr =$("#addr_address").val();
				var region =$("#region_address").text();
				if(addr && addr.length>0 &&region && region.length>0){
					address.saveAddress();
				}else{
					common.showTip({
						content: "请输入完整地址"
					});
				}
				
			});
		},
		saveAddress: function() {
			var param = {
				"openId": window.localStorage.getItem("openId"),
				"appId": window.localStorage.getItem("appId"),
				"address": {
					"receiverProvinceId": $("#province_address").attr("receiverProvinceId"),
					"receiverRegionId": $("#region_address").attr("receiverRegionId"),
					"receiverCity": $("#city_address").text(),
					"receiverCityId": $("#city_address").attr("receiverCityId"),
					"receiverAddr": $("#addr_address").val(),
					"receiverRegion": $("#region_address").text(),
					"receiverProvince": $("#province_address").text()
				}
			}
			common.loadData({
				url: "/wxmember/service/changeUserInfo",
				param: param,
				successCallBack: function(res) {
					if(res.status == "0"){
					common.showTip({
						content: "修改成功"
					});
//					window.location.href = "#/index/myProfile";
						window.history.back(-1);
					}
				}, //回调函数
				dataSource: "getUserInfo",
				cache: false
			});
		},
		initPicker: function() {
			var cityPicker3 = new mui.PopPicker({
				layer: 3,
				changeFn: function(even) {
					var nextPickerElement = this.nextSibling;
					if(nextPickerElement && nextPickerElement.picker) {
						var eventData = event.detail || {};
						var preItem = eventData.item || {};
						if(preItem.value) {
							var param = {
								"appId": window.localStorage.getItem("appId"),
								pid: preItem.value
							}
							common.loadData({
								url: "/wxmember/service/getCityList",
								param: param,
								successCallBack: function(json) {
									console.log("调用后台ajax返回值为：" + JSON.stringify(json));
									if(json.status == "0") {
										nextPickerElement.picker.setItems(json.data);
									} else {
										common.showTip({
											content: json.message
										});
									}
								}, //回调函数
								dataSource: "getCityList"
							});
						} else {
							nextPickerElement.picker.setItems([]);
						}
					}
				}
			});
			cityPicker3.setData(address.provinceList);
			cityPicker3.show(function(selectItems) {

				$("#province_address").text(selectItems[0].text);
				$("#province_address").attr("receiverProvinceId", selectItems[0].value);
				$("#city_address").text(selectItems[1].text);
				$("#city_address").attr("receiverCityId", selectItems[1].value);
				$("#region_address").text(selectItems[2].text);
				$("#region_address").attr("receiverRegionId", selectItems[2].value);

				cityPicker3.dispose();
			}, function() {
				cityPicker3.dispose();
			});
		},
		/**
		 * 进行页面路由监听配置
		 */
		bindRout: function() {
			//路由
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: "left"
				});
			});
			Path.listen();
		},
		/**
		 * 页面数据加载
		 */
		initData: function() {
			var straddress = window.localStorage.getItem("address");
			if(straddress && straddress !="undefined") {
				var addressObj = JSON.parse(straddress);
				$("#province_address").text(addressObj.receiverProvince);
				$("#province_address").attr("receiverProvinceId", addressObj.receiverProvinceId);
				$("#city_address").text(addressObj.receiverCity);
				$("#city_address").attr("receiverCityId", addressObj.receiverCityId);
				$("#region_address").text(addressObj.receiverRegion);
				$("#region_address").attr("receiverRegionId", addressObj.receiverRegionId);
				$("#addr_address").val(addressObj.receiverAddr);
			}
			var param = {
				"appId":window.localStorage.getItem("appId"),
			}
			common.loadData({
				url: "/wxmember/service/getProvinceList",
				param: param,
				successCallBack: function(json) {
					console.log("调用后台ajax返回值为：" + JSON.stringify(json));
					if(json.status == "0") {
						address.provinceList = json.data;

					} else {
						common.showTip({
							content: json.message
						});
					}
				}, //回调函数
				dataSource: "getCityList"
			});
		},
		//callback function
		getaddressListCallBack: function(json) {
			console.log("调用后台ajax返回值为：" + JSON.stringify(json));
			if(json.status == "0") {
				address.data = json;
				var html2 = template('addressTemplate', address.data);
				document.getElementById('main_address').innerHTML = html2;
			} else {
				common.showTip({
					content: json.message
				});
			}

		},

		/**
		 * each page init mothed
		 */
		initialize: function() {
			//动态加载数据
			this.initData();
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = address;
});